#!/usr/bin/perl

use warnings;

$0=~m/^(.+)\/[^\/]+$/;
require "$1/config.pl";

my $dbh = db_connect();
my ($d,$m,$y) = (localtime)[3,4,5];
$m++;$y+=1900;
$dbh->do("insert into access_log_sum(date,user_id,client_ip,size,url,cnt) select DATE_FORMAT(date,'%Y-%m-%d'),users.id,client_ip,sum(size),url,count(*) FROM access_log, users WHERE users.user=access_log.user AND date<'$y-$m-$d'  GROUP BY client_ip,url,users.id ;") || die "db error: $^E";
$dbh->do("delete from access_log where date<'$y-$m-$d'") || die "db error: $^E";

if ($cfg_root_url) {
  my ($cmd1, $cmd2);
  foreach (split(/;/, $cfg_root_url)) {

    $cmd1 = "insert into access_log_sum(date,user_id,client_ip,size,url,cnt) " .
            "select DATE_FORMAT(date,'%Y-%m-%d'),user_id,client_ip,sum(size),'$_',sum(cnt) " .
            "FROM access_log_sum WHERE url like '%.$_' " .
            "GROUP BY client_ip,user_id,date;";
    $cmd2 = "delete from access_log_sum where url like '%.$_'";

    $dbh->do($cmd1) || die "db error: $^E";
    $dbh->do($cmd2) || die "db error: $^E";
  }
}

$dbh->disconnect;

